package tnviet.proj.jna.analyzer;
import jpcap.packet.*;
import java.util.*;
import tnviet.proj.jna.Program;
import tnviet.proj.jna.utilities.ResourceManager;

public class ICMPAnalyzer extends JNPacketAnalyzer
{
    private static ResourceManager resourceManager = new ResourceManager(JNPacketAnalyzer.class, Program.locale);
	private static final String[] valueNames={
		resourceManager.getString("icmp.type"),
		resourceManager.getString("icmp.code"),
		resourceManager.getString("icmp.id"),
		resourceManager.getString("icmp.sequence"),
		resourceManager.getString("icmp.redirectAdd"),
		resourceManager.getString("icmp.addressMask"),
		resourceManager.getString("icmp.originalTimestamp"),
		resourceManager.getString("icmp.receiveTimestamp"),
		resourceManager.getString("icmp.transmissionTimestamp")
	};
	
	private static final String[] typeNames={
		resourceManager.getString("icmp.echoReply0"),
		resourceManager.getString("icmp.unknown1"),
		resourceManager.getString("icmp.unknown2"),
		resourceManager.getString("icmp.destUnreachable3"),
		resourceManager.getString("icmp.sourceQuench4"),
		resourceManager.getString("icmp.redirect5"),
		resourceManager.getString("icmp.unknown6"),
		resourceManager.getString("icmp.unknown7"),
		resourceManager.getString("icmp.echo8"),
		resourceManager.getString("icmp.unknown9"),
		resourceManager.getString("icmp.unknown10"),
		resourceManager.getString("icmp.timeExceeded11"),
		resourceManager.getString("icmp.parameterProblem12"),
		resourceManager.getString("icmp.timestamp13"),
		resourceManager.getString("icmp.timestampReply14"),
		resourceManager.getString("icmp.unknown15"),
		resourceManager.getString("icmp.unknown16"),
		resourceManager.getString("icmp.addMaskRequest17"),
		resourceManager.getString("icmp.addMaskRequest18")
	};
	
	private Hashtable values=new Hashtable();
	
	public ICMPAnalyzer(){
		layer=TRANSPORT_LAYER;
	}
	
	public boolean isAnalyzable(Packet p){
		return (p instanceof ICMPPacket);
	}
	
	public String getProtocolName(){
		return resourceManager.getString("icmp");
	}
	
	public String[] getValueNames(){
		return valueNames;
	}
	
	public void analyze(Packet p){
		if(!isAnalyzable(p)) return;
		values.clear();
		
		ICMPPacket icmp=(ICMPPacket)p;
		if(icmp.type>=typeNames.length){
			values.put(valueNames[0],String.valueOf(icmp.type));
		}else{
			values.put(valueNames[0],typeNames[icmp.type]);
		}
		values.put(valueNames[1],new Integer(icmp.code));
		
		if(icmp.type==0 || icmp.type==8 || (icmp.type>=13 && icmp.type<=18)){
			values.put(valueNames[2],new Integer(icmp.id));
			values.put(valueNames[3],new Integer(icmp.seq));
		}
		
		if(icmp.type==5)
			values.put(valueNames[4],icmp.redir_ip);
		
		if(icmp.type==17 || icmp.type==18)
			values.put(valueNames[5],(icmp.subnetmask>>12)+"."+
			                         ((icmp.subnetmask>>8)&0xff)+"."+
			                         ((icmp.subnetmask>>4)&0xff)+"."+
			                         (icmp.subnetmask&0xff)+".");
		
		if(icmp.type==13 || icmp.type==14){
			values.put(valueNames[6],new Long(icmp.orig_timestamp));
			values.put(valueNames[7],new Long(icmp.recv_timestamp));
			values.put(valueNames[8],new Long(icmp.trans_timestamp));
		}
	}
	
	public Object getValue(String valueName){
		return values.get(valueName);
	}
	
	public Object getValueAt(int index){
		if(index<0 || index>=valueNames.length) return null;
		return values.get(valueNames[index]);
	}
	
	public Object[] getValues(){
		Object[] v=new Object[valueNames.length];
		
		for(int i=0;i<valueNames.length;i++)
			v[i]=values.get(valueNames[i]);
		
		return v;
	}
        @Override
        public String getValueNameAt(int index) {
            return valueNames[index];
        }
}
